package cn.slf.ph.dao.impl;

import cn.slf.ph.dao.GoodsDao;
import cn.slf.ph.entity.Goods;
import cn.slf.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public List<Goods> selectAll(Map<String, String> map) {
        List<Goods> list=new ArrayList<>();
        //形成动态的sql语句
        String sql="select * from goods where 1=1 ";
        //用于存放注入数据
        List<Object> params=new ArrayList<>();
        //是否按照商品状态查询
        String goodsStatus=map.get("goodsStatus");
        if (goodsStatus!=null&&!"".equals(goodsStatus)){
            sql+=" and goodsStatus=? ";
            params.add(goodsStatus);
        }
        //是否按照商品名查询
        String goodsName=map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
             sql+=" and goodsName like ? ";
             params.add("%"+goodsName+"%");
        }
        //是否按照商店名称查询
        String shopName = map.get("shopName");
        if (shopName!=null&&!"".equals(shopName)){
            sql+=" and goodsShopName like ? ";
            params.add("%"+shopName+"%");
        }
        //是否按照时间查询
        String startTime=map.get("startTime");
        if (startTime!=null&&!"".equals(startTime)){
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        //判断是否按照id排序
        sql+=" order by goodsId desc";

        //是否按照分页查询
        String pn = map.get("pageNum");
        if (pn!=null&&!"".equals(pn)){
            sql+=" limit ?,? ";
            int pageNum=Integer.parseInt(pn);
            int pageSize=Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object obj[]=params.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!resultSet.next()) break;
                //形成新对象
                Goods goods=new Goods(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4),
                        resultSet.getInt(5),
                        resultSet.getDate(6),
                        resultSet.getInt(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getString(10),
                        resultSet.getString(11),
                        resultSet.getInt(12)
                );
                list.add(goods);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int getTotal(Map<String, String> map) {
        //形成动态的sql语句
        String sql="select count(1) from goods where 1=1 ";
        //用于存放注入数据
        List<Object> params=new ArrayList<>();
        //是否按照商品名查询
        String goodsName=map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
            sql+=" and goodsName like ? ";
            params.add("%"+goodsName+"%");
        }
        //是否按照商店名称查询
        String shopName = map.get("shopName");
        if (shopName!=null&&!"".equals(shopName)){
            sql+=" and goodsShopName like ? ";
            params.add("%"+shopName+"%");
        }
        //是否按照时间查询
        String startTime=map.get("startTime");
        if (startTime!=null&&!"".equals(startTime)){
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        //判断是否按照id排序
        sql+=" order by goodsId desc";

        Object obj[]=params.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, obj);
        int total=0;
        while (true) {
            try {
                if (!resultSet.next()) break;
                total=resultSet.getInt(1);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        JDBCDruidUtil.close();
        return total;
    }

    @Override
    public int ModifyGoodsStatus(Map<String, String> map) {

        String sql=" update goods set goodsStatus= ? where goodsId= ? ";
        //
        String Id = map.get("goodsId");
        int goodsId=Integer.parseInt(Id);
        // 参数
        List<Object> params=new ArrayList<>();
        //
        String Status = map.get("goodsStatus");
        int goodsStatus=Integer.parseInt(Status);
        params.add(goodsStatus);
        //
//        if (goodsStatus==1){
//            params.add(0);
//        }else {
//            params.add(1);
//        }
        params.add(goodsId);
        Object obj[]=params.toArray();
        int num=JDBCDruidUtil.update(sql,obj);
        return num;

    }
}
